// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import iView from 'iview';
import iviewArea from 'iview-area';
import App from './App';
import router from './router';
import axios from 'axios';
import Cookies from 'js-cookie';
import _global from './common/Consts';
import 'iview/dist/styles/iview.css';
// import '../my-theme/dist/iview.css';
import './assets/css/style.css';

Vue.config.productionTip = false;

// Vue.use(axios);
Vue.prototype.$axios = axios;
axios.defaults.baseURL = 'http://localhost:8080';
Vue.prototype.$Cookies = Cookies;
Vue.prototype.GLOBAL = _global;
Vue.use(iView);
Vue.use(iviewArea);

router.beforeEach((to, from, next) => {
  iView.LoadingBar.start();
  if (to.path.indexOf('admin') > 0) {
    if (!Cookies.get('access') && to.name !== 'AdminLogin') { // 判断是否已经登录且前往的页面不是登录页
      next({
        name: 'AdminLogin'
      });
    } else if (Cookies.get('access') && to.name === 'AdminLogin') { // 判断是否已经登录且前往的是登录页
      next({
        name: 'Admin'
      });
    } else {
      next();
    }
  } else if (to.path.indexOf('user') > 0 || to.path.indexOf('pay') > 0) {
    if (!Cookies.get('customer')) {
      next({
        name: 'Login'
      });
    } else {
      next();
    }
  } else {
    next();
  }
});

router.afterEach((to) => {
  iView.LoadingBar.finish();
  window.scrollTo(0, 0);
});

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  Cookies,
  components: {App},
  template: '<App/>'
});
